package customerEnter.Frame.JDBCDao.Processor;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.jdbc.support.rowset.SqlRowSet;

import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.DaoDataTransmission.PagingParametersResult;
import customerEnter.Frame.Error.FramErrorProcessing;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.bean.FrameBeansModels;
import customerEnter.Frame.scanning.ScanningExtendedConfig;
import customerEnter.Frame.scanning.scanningServer.ProjectScanningModelServer;
import customerEnter.tool.WhetherToEmpty;

/**
 * 轉換成list數據
 * 
 * 类名称：ListProcessorImpl 类描述： 创建人：dy 创建时间：2016-4-30 上午10:10:42
 * 
 */
@SuppressWarnings({ "unchecked" })
public class ProcessorModelImpl implements ResultProcessor {
	/**
	 * 設置表結構和相對應的類結構
	 * 
	 * @throws FramErrorProcessing
	 * 
	 * @throws Exception
	 */
	@SuppressWarnings("rawtypes")
	private <T, K> void NewInstanceValues(DaoFramePameTransmission<T, K> daoFramePameTransmission)
			throws FramErrorProcessing {
		Class<?> class1 = daoFramePameTransmission.cls;
		daoFramePameTransmission.setPagingParametersResult(new PagingParametersResult());
		ScanningExtendedConfig scanningExtendedConfig = new ProjectScanningModelServer();
		FrameBeansModels frameBeansModel = new FrameBeansModels();
		scanningExtendedConfig.getFrameBeansModels(class1.getDeclaredFields(), frameBeansModel);
		SqlRowSet set = daoFramePameTransmission.sqlRowSet;

		while (set.next()) {
			Object object;
			try {
				object = class1.newInstance();
				Field[] fields = frameBeansModel.getFields();
				// 設置類的基本屬性
				setFilesModel(set, fields, object, daoFramePameTransmission);
				// 反射基本類
				setFilesObject(frameBeansModel, class1, object, set, daoFramePameTransmission);
				// 放入list
				daoFramePameTransmission.DaoFramelistOther.add(object);
			} catch (InstantiationException e) {
				throw new FramErrorProcessing(e);
			} catch (IllegalAccessException e) {
				throw new FramErrorProcessing(e);
			}

		}

	}

	public <T, K> void setFilesModel(SqlRowSet rSet, Field[] fields, Object object,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) throws FramErrorProcessing {

		for (Field field : fields) {
			if (WhetherToEmpty.ObjectWhetherToEmpty(field)) {
				continue;
			}
			Object objectValue = null;
			String fieldName = field.getName();
			objectValue = rSet.getObject(fieldName);
			try {
				if (WhetherToEmpty.ObjectWhetherToEmpty(objectValue)) {
					continue;
				}
				if ("double".equals(field.getType().getName())) {
					try {
						Double object2 = (Double) objectValue;
						field.set(object, object2);
					} catch (Exception e) {
						BigDecimal object2 = (BigDecimal) objectValue;
						field.set(object, object2);
					}
				} else {
					Object object2 = objectValue;// 數據庫對於的值
					field.set(object, object2);
				}
			} catch (IllegalArgumentException e1) {
				throw new FramErrorProcessing(e1, "屬性反射錯誤：" + e1.getMessage() + " 屬性名字：" + fieldName + " 屬性值："
						+ objectValue);
			} catch (IllegalAccessException e1) {
				throw new FramErrorProcessing(e1);
			}
			/**
			 * 分頁數據 總數統計
			 * 
			 */
			if (daoFramePameTransmission.pagingParametersCount == true) {
				if (daoFramePameTransmission.getPagingParametersResult().getPagingParametersCount() <= 0) {
					Object vaObject = daoFramePameTransmission.sqlRowSet.getObject("pagingParametersCount");
					daoFramePameTransmission.getPagingParametersResult().setPagingParametersCount(
							Integer.parseInt(vaObject.toString()));
				}
			}

		}
	}

	public <T, K> void setFilesObject(FrameBeansModels frameBeansModel, Class<?> class1, Object object, SqlRowSet set,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) throws FramErrorProcessing {

		Map<String, FrameBeansModels> map2 = frameBeansModel.getFrameBeansMap();
		for (Entry<String, FrameBeansModels> entry : map2.entrySet()) {

			Field field;
			try {
				field = class1.getDeclaredField(entry.getKey());
				Object object2 = Class.forName(entry.getKey()).newInstance();
				Field[] fields2 = entry.getValue().getFields();
				setFilesModel(set, fields2, object2, daoFramePameTransmission);
				field.set(object, object2);
				if (!WhetherToEmpty.ObjectWhetherToEmpty(entry.getValue().getFrameBeansMap())) {
					FrameBeansModels frameBeansModels = entry.getValue();
					setFilesObject(frameBeansModels, class1, object2, set, daoFramePameTransmission);
				}
			} catch (InstantiationException e) {
				throw new FramErrorProcessing(e);
			} catch (IllegalAccessException e) {
				throw new FramErrorProcessing(e);
			} catch (ClassNotFoundException e) {
				throw new FramErrorProcessing(e);
			} catch (NoSuchFieldException e) {
				throw new FramErrorProcessing(e);
			}

		}
	}

	/**
	 * 循環賦值給類 返回list數據
	 */
	public <T, K> void ProcessorList(DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		Log4j.setLog4j(this.getClass());
		try {
			NewInstanceValues(daoFramePameTransmission);
		} catch (FramErrorProcessing e) {
			Log4j.Log4JOutError(e.getMessage());
		}
	}

	/**
	 * 返回 單個Object
	 */
	public <T, K> void ProcessorObject(DaoFramePameTransmission<T, K> daoFramePameTransmission) {

	}

}
